package myky.com.doctorapp.utils;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.provider.ContactsContract;
import android.text.TextUtils;
import android.widget.Toast;

import com.foamtrace.photopicker.ImageCaptureManager;
import com.foamtrace.photopicker.SelectModel;
import com.foamtrace.photopicker.intent.PhotoPickerIntent;
import com.github.nukc.stateview.Constant;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.Permission;

import java.util.ArrayList;
import java.util.List;

import myky.com.doctorapp.R;
import myky.com.doctorapp.camera.MedicalTakePhotoActivity;
import myky.com.doctorapp.ui.UserMedicalRecordActivity;

/**
 * Description:
 *
 * @author Mr.li
 * Ctreated by 2019/2/15 14:38
 */
public class AddPhotoUtils {

    private Context mContext;

    /**
     * 预约，远程、咨询图片获取方式
     *
     * @param activity
     * @param limit
     * @param chooseType
     * @param cameraLimit
     * @param photoLists
     * @param type
     */
    public static void getPhotos(final Activity activity, final int limit, final int chooseType, final int cameraLimit, final ArrayList<String> photoLists, int type) {

        DialogChoice.setDialog_ChoosePhoto(chooseType, activity, cameraLimit, new DialogChoice.OnChoosePhotoListener() {
            @Override
            public void onChoosePhotoCallback(int onclick) {
                switch (onclick) {
                    case Constants.CASE_MEDICALRECORD:
                        Intent intent = new Intent();
                        intent.setClass(activity, UserMedicalRecordActivity.class);
                        activity.startActivity(intent);
                        break;

                    case Constants.TAKE_CAMERA:
                        AndPermission.with(activity)
                                .runtime()
                                .permission(Permission.CAMERA, Permission.READ_EXTERNAL_STORAGE,Permission.WRITE_EXTERNAL_STORAGE)
                                // .rationale(new RuntimeRationale())
                                .onGranted(permissions -> {
                                    Intent intent_camera = new Intent();
                                    intent_camera.setClass(activity, MedicalTakePhotoActivity.class);
                                    intent_camera.putExtra("TypeName", "");
                                    activity.startActivityForResult(intent_camera, Constants.TAKE_CAMERA);
                                })
                                .onDenied(permissions -> {
                                    if (AndPermission.hasAlwaysDeniedPermission(activity, permissions)) {
                                        showSettingDialog(activity, permissions);
                                    }
                                })
                                .start();
                        break;

                    case Constants.TAKE_ALBUM:
                        AndPermission.with(activity)
                                .runtime()
                                .permission(Permission.CAMERA, Permission.READ_EXTERNAL_STORAGE)
                                // .rationale(new RuntimeRationale())
                                .onGranted(permissions -> {
                                    PhotoPickerIntent intent_album = new PhotoPickerIntent(activity);
                                    intent_album.setSelectModel(SelectModel.MULTI);
                                    // 是否显示拍照
                                    intent_album.setShowCarema(false);
                                    // 最多选择照片数量，默认为9
                                    intent_album.setMaxTotal(cameraLimit);
                                    // 已选中的照片地址， 用于回显选中状态
                                    intent_album.setSelectedPaths(photoLists);
                                    activity.startActivityForResult(intent_album, Constants.TAKE_ALBUM);
                                })
                                .onDenied(permissions -> {
                                    if (AndPermission.hasAlwaysDeniedPermission(activity, permissions)) {
                                        showSettingDialog(activity, permissions);
                                    }
                                })
                                .start();
                        break;
                     default:
                         break;
                }
            }

        });
    }

    /**
     * Display setting dialog.
     */
    public static void showSettingDialog(Context context, final List<String> permissions) {
        List<String> permissionNames = Permission.transformText(context, permissions);
        String message = context.getString(R.string.message_permission_always_failed, TextUtils.join("\n", permissionNames));
        new AlertDialog.Builder(context)
                .setCancelable(false)
                .setTitle("")
                .setMessage(message)
                .setPositiveButton(R.string.setting, (dialog, which) -> setPermission(context))
                .setNegativeButton(R.string.cancel, (dialog, which) -> {
                })
                .show();
    }

    /**
     * Set permissions.
     */
    private static void setPermission(Context context) {
        AndPermission.with(context)
                .runtime()
                .setting()
                .start();
    }

}
